﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace BaseFunction
{   
    /// <summary>
    /// 通用方法
    /// </summary>
    public class CommonClass
    {

        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            string hostName = Dns.GetHostName();//本机名   
            System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址，包括IPv4和IPv6   
            return addressList[1].ToString();
        }

        /// <summary>
        /// 获取用户登录的CIP
        /// </summary>
        /// <returns></returns>
        public static string GetCIP()
        {
            string infor = "";
            try
            {
                string url = "http://pv.sohu.com/cityjson";
                WebRequest wRequest = WebRequest.Create(url);
                WebResponse wResponse = wRequest.GetResponse();
                Stream stream = wResponse.GetResponseStream();
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
                infor = reader.ReadToEnd();
                wResponse.Close();
            }
            catch (Exception ex)
            {

            }
            return infor;
        }
        /// <summary>
        /// 根据cip获取城市基本信息
        /// </summary>
        /// <param name="cip"></param>
        /// <returns></returns>
        public static string GetCityInforByCip(string cip)
        {
            string infor = "";
            try
            {
                string url = "http://apistore.baidu.com/microservice/iplookup?ip=" + cip;
                WebRequest wRequest = WebRequest.Create(url);
                WebResponse wResponse = wRequest.GetResponse();
                Stream stream = wResponse.GetResponseStream();
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
                infor = reader.ReadToEnd();
                wResponse.Close();
            }
            catch (Exception ex)
            {

            }
            return infor;
        }
        /// <summary>
        /// 日志操作类型
        /// </summary>
        public enum OperationType
        {
            login = 1,
            register = 2,
            update = 3,
            delete = 4
        }

        #region 发送短信
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phonenumner">手机号码</param>
        /// <param name="code">验证码</param>
        public static bool SendMessage(string phonenumner, string message)
        {
            string account = BaseFunction.StaticTableClass.SystemConfigHelp.GetSystemConfigValue("account");//短信接口用户名
            string pswd = BaseFunction.StaticTableClass.SystemConfigHelp.GetSystemConfigValue("pswd");//短信接口密码
           
            bool flag = true;
            try
            {
                string url = "http://222.73.117.158/msg/HttpBatchSendSM?account=" + account + "&pswd=" + pswd + "&mobile=" + phonenumner + "&msg=" + message + "&needstatus=true";
                WebRequest wRequest = WebRequest.Create(url);
                WebResponse wResponse = wRequest.GetResponse();
                Stream stream = wResponse.GetResponseStream();
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
                string r = reader.ReadToEnd();
                wResponse.Close();
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }
        #endregion
    }
}
